package com.umerclass.ui.anmo;

import android.graphics.Color;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.umerclass.AppDroid;
import com.umerclass.R;
import com.umerclass.bean.AddbankBean;
import com.umerclass.bean.anmo.BankListBean;
import com.umerclass.bean.anmo.BrokerageListBean;
import com.umerclass.bean.anmo.PerformanceListBean;
import com.umerclass.frame.model.InfoResult;
import com.umerclass.frame.model.MsgBean;
import com.umerclass.frame.okhttp.OkEntityListRequest;
import com.umerclass.frame.okhttp.OkEntityRequest;
import com.umerclass.frame.okhttp.OkSimpleRequest;
import com.umerclass.frame.ui.activity.BaseActivity;
import com.umerclass.utils.Constants;
import com.umerclass.utils.EventConstants;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import butterknife.BindView;
import butterknife.OnClick;

public class TiXianActivity extends BaseActivity {

    @BindView(R.id.linear_back)
    LinearLayout linearBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv1)
    TextView tv1;
    @BindView(R.id.tv2)
    TextView tv2;
    @BindView(R.id.tv3)
    TextView tv3;
    @BindView(R.id.tv_money)
    TextView tvMoney;
    @BindView(R.id.tv_money_desc)
    TextView tvMoneyDesc;
    @BindView(R.id.et_num)
    EditText etNum;
    @BindView(R.id.tv_all)
    TextView tvAll;
    @BindView(R.id.tv_card_name)
    TextView tvCardName;
    @BindView(R.id.tv_card)
    TextView tvCard;
    @BindView(R.id.linear_card)
    LinearLayout linearCard;
    @BindView(R.id.linear_card_info)
    LinearLayout linear_card_info;
    @BindView(R.id.linear_one)
    LinearLayout linearOne;
    @BindView(R.id.linear_two)
    LinearLayout linearTwo;
    @BindView(R.id.btn_sure)
    Button btnSure;
    private int index = 1;//1代理佣金 2仪器分润
    private String money = "";
    private boolean clickone= false;
    private String money1 = "";//代理佣金
    private String money2 = "";//仪器分润
    private boolean clickTwo= false;
    private List<BankListBean> bankListBeans = new ArrayList<>();
    private String regex = "(\\w{4})(.*)(\\w{4})";

    private String bankId = "";
    private String bankNo = "";
    private String bankName = "";
    private int type = 1;//银行卡提现 2微信提现
    private String payPassword = "";

    @Override
    public boolean isImmersionBarEnabled() {
        return false;
    }

    @Override
    public boolean isSupportSwipeBack() {
        return false;
    }

    @Override
    public int getLayoutResId() {
        return R.layout.activity_anmo_tixian;
    }

    @Override
    public void init() {
        Window window = getWindow();
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        window.setStatusBarColor(getResources().getColor(R.color.white));
        payPassword = super.getIntent().getStringExtra("password");
        tv1.setBackgroundResource(R.drawable.shape_four_cicle);
        tv2.setBackgroundResource(R.drawable.shape_four2);
        tv1.setTextColor(Color.rgb(196,36,43));
        tv2.setTextColor(Color.rgb(255,255,255));
        linear_card_info.setVisibility(View.GONE);
        tvCard.setText("去绑定银行卡");
        etNum.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if(!TextUtils.isEmpty(s.toString())){
                    money = s.toString();
                    btnSure.setBackgroundResource(R.drawable.shape_one_press);
                }else {
                    money = "";
                    btnSure.setBackgroundResource(R.drawable.shape_one_normal);
                }
                clickOne();
            }
        });

        getMoneyOne();
        getBanks();
    }

    @Override
    public void loadData() {

    }

    @OnClick({R.id.linear_back,R.id.tv1,R.id.tv2,R.id.linear_card,R.id.btn_sure,R.id.tv3})
    public void OnClick(View view) {
        switch (view.getId()) {
            case R.id.btn_sure:
                if(type==1){
                    if(clickone){
                        if(money1!=null && !money1.equals("null")&& !money1.equals("")&& !money1.equals(" ")){
                            double a1 = Double.valueOf(money);
                            double a2 = Double.valueOf(money1);
                            if(a1<10){
                                showToast("提现金额最低10元");
                            }else if(a1>a2){
                                showToast("提现金额大于佣金");
                            }else {
                                showProgress("正在请求...");
                                OkSimpleRequest request = new OkSimpleRequest<>(R.id.brokerageSubmitPrice,
                                        Constants.brokerageSubmitPrice);
                                request.addParams("userId", AppDroid.getInstance().getUserOtherBean().getUserId());
                                request.addParams("bankId", bankId);
                                request.addParams("price", money);
                                request.addParams("payPassword", payPassword);
                                request.addHeadParams("userType",AppDroid.getInstance().getUserOtherBean().getUserType());
                                request.addHeadParams("userToken",AppDroid.getInstance().getUserOtherBean().getToken());
                                requestOkhttpSimple(request);
                            }
                        }else {
                            showToast("没有佣金可以提现");
                        }

                    }
                }
                break;
            case R.id.linear_back:
                finish();
                break;
            case R.id.tv1:
                //可提现佣金
                type = 1;
                        tv1.setBackgroundResource(R.drawable.shape_four_cicle);
                tv2.setBackgroundResource(R.drawable.shape_four2);
                tv1.setTextColor(Color.rgb(196,36,43));
                tv2.setTextColor(Color.rgb(255,255,255));
                money = "";
                etNum.setText("");
                linearOne.setVisibility(View.VISIBLE);
                linearTwo.setVisibility(View.GONE);
                tvMoney.setText(money1);
                clickOne();
                break;
            case R.id.tv2:
                type = 2;
                tv2.setBackgroundResource(R.drawable.shape_four_cicle);
                tv1.setBackgroundResource(R.drawable.shape_four2);
                tv2.setTextColor(Color.rgb(196,36,43));
                tv1.setTextColor(Color.rgb(255,255,255));
                money = "";
                etNum.setText("");
                linearOne.setVisibility(View.GONE);
                linearTwo.setVisibility(View.VISIBLE);
                tvMoney.setText(money2);
                break;
             //绑定银行卡
            case R.id.linear_card:
                switchTo(TiXianActivity.this,BangDingCardActivity.class,false);
                break;
            //提现明细
            case R.id.tv3:
                switchTo(TiXianActivity.this,TiXianDetailsActivity.class,false);
                break;
            default:
                break;
        }
    }

    @Override
    public void handlerMeg(MsgBean msgBean) {
        super.handlerMeg(msgBean);
        if(msgBean.getFlag() == EventConstants.addcardSuccess){
            AddbankBean addbankBean = (AddbankBean) msgBean.getObject();
            bankId = String.valueOf(addbankBean.getBankId());
            bankNo = addbankBean.getBankNo();
            bankName = addbankBean.getBankName();
            Matcher m = Pattern.compile(regex).matcher(addbankBean.getBankNo());
            if (m.find()) {
                String rep = m.group(2);
                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < rep.length(); i++) {
                    sb.append("*");
                }
                String txt = addbankBean.getBankNo().replaceAll(rep, sb.toString());
                tvCardName.setText(bankName +" "+ txt);
            }
            clickOne();
        }
    }

    @Override
    protected void onSuccess(int what, InfoResult t) {
        switch (what) {
            case R.id.brokerageSubmitPrice:
                showToast(t.getDesc());
                etNum.setText("");
                money = "";
                hideProgress();
                MsgBean msgBean = new MsgBean();
               /* msgBean.setObject(1);
                msgBean.setFlag(EventConstants.tixianSuccess);
                getEventBus().post(msgBean);*/
                getMoneyOne();
                break;
            case R.id.myBrokerageList:
                BrokerageListBean brokerageListBean = (BrokerageListBean) t.getT();
                if(brokerageListBean!=null){
                    money1 = brokerageListBean.getTotal();
                    tvMoney.setText(money1);
                }
                getMoneyTwo();
                break;
            case R.id.myPerformanceList:
                PerformanceListBean performanceListBean = (PerformanceListBean) t.getT();
                if(performanceListBean!=null){
                    money2 = performanceListBean.getTotal();
                }
                break;
            case R.id.bankList:
                bankListBeans = (List<BankListBean>) t.getT();
                if(bankListBeans!=null && bankListBeans.size()>0){
                    linear_card_info.setVisibility(View.VISIBLE);
                    BankListBean bankListBean = bankListBeans.get(0);
                    bankId = String.valueOf(bankListBean.getBankId());
                    bankNo = bankListBean.getBankNo();
                    bankName = bankListBean.getBankName();
                    Matcher m = Pattern.compile(regex).matcher(bankListBean.getBankNo());
                    if (m.find()) {
                        String rep = m.group(2);
                        StringBuilder sb = new StringBuilder();
                        for (int i = 0; i < rep.length(); i++) {
                            sb.append("*");
                        }
                        String txt = bankListBean.getBankNo().replaceAll(rep, sb.toString());
                        tvCardName.setText(bankName +" "+ txt);
                    }
                }
                break;
            default:
                break;
        }
    }

    @Override
    protected void onFail(int what, InfoResult t) {
        switch (what) {
            case R.id.brokerageSubmitPrice:
                hideProgress();
                showToast(t.getDesc());
                break;
                default:break;
        }
    }

    /**
     * 获取代理佣金
     */
    private void getMoneyOne(){
        OkEntityRequest<BrokerageListBean> request = new OkEntityRequest<>(R.id.myBrokerageList,
                Constants.myBrokerageList, BrokerageListBean.class);
        request.addParams("userId", AppDroid.getInstance().getUserOtherBean().getUserId());
        //0入账 1出账
        request.addParams("operation", 0);
        request.addParams("startDayTime", "");
        request.addParams("endDayTime", "");
        request.addParams("page",1);
        request.addParams("rows",1);
        request.addHeadParams("userType",AppDroid.getInstance().getUserOtherBean().getUserType());
        request.addHeadParams("userToken",AppDroid.getInstance().getUserOtherBean().getToken());
        requestOkhttpEntity(request);
    }

    /**
     * 仪器分润
     */
    private void getMoneyTwo(){
        OkEntityRequest<PerformanceListBean> request = new OkEntityRequest<>(R.id.myPerformanceList,
                Constants.myPerformanceList, PerformanceListBean.class);
        request.addParams("userId", AppDroid.getInstance().getUserOtherBean().getUserId());
        //0入账 1出账
        request.addParams("operation", 0);
        request.addParams("startDayTime", "");
        request.addParams("endDayTime", "");
        request.addParams("page",1);
        request.addParams("rows",1);
        request.addHeadParams("userType",AppDroid.getInstance().getUserOtherBean().getUserType());
        request.addHeadParams("userToken",AppDroid.getInstance().getUserOtherBean().getToken());
        requestOkhttpEntity(request);
    }

    private void getBanks(){
        OkEntityListRequest<BankListBean> request = new OkEntityListRequest<>(R.id.bankList,
                Constants.bankList, BankListBean.class);
        request.addParams("userId", AppDroid.getInstance().getUserOtherBean().getUserId());
        request.addHeadParams("userType",AppDroid.getInstance().getUserOtherBean().getUserType());
        request.addHeadParams("userToken",AppDroid.getInstance().getUserOtherBean().getToken());
        requestOkhttpEntityList(request);
    }

    private void clickOne(){
        if(money1==null || TextUtils.isEmpty(money)|| money1.equals("") || money1.equals(" ") ||
        bankId.equals("") || bankNo.equals("")){
            clickone = false;
            btnSure.setBackgroundResource(R.drawable.shape_one_normal);
        }else {
            clickone = true;
            btnSure.setBackgroundResource(R.drawable.shape_one_press);
        }
    }

}
